<template>
	<view class="content">
		<image src="../../static/common/aui-icon-close.png" class="close" @tap.stop="closed"></image>
		<view class="page-section swiper">
				<view class="page-section-spacing">
						<swiper class="swiper" 
						:indicator-dots="indicatorDots" 
						loop="true" circular="true" 
						:autoplay="autoplay" 
						:interval="interval" 
						:duration="duration">
								<swiper-item class="box" v-for="(item,index) in list" :key="index">
									<view class="big-title">{{item.bigTitle}}</view>
									<view class="small-title">{{item.smallTitle}}</view>
									<image :src="item.mainPic" class="mainPic"></image>
									<image src="../../static/login/panda.png" class="panda"></image>
								</swiper-item>
						</swiper>
				</view>
		</view>
		<view class="login-btn" @click="userLogin">
			<image class="wx-login-icon" src="../../static/login/wx-login-icon.png"></image>
			<view class="wx-login-text" >微信免注册登录</view>
		</view>
		<view class="login-type-content" :class="{'direction':isTrue}">
			<view class="login-type phoneLogin" @tap="loginPhone">
				<image src="../../static/common/phone.png" class="icon-login"></image>
				<text>手机/账号登录</text>
			</view>
			<view class="login-type appleLogin" @tap="loginApple" v-if="isTrue" >
				<image src="../../static/common/apple.png" class="icon-apple"></image>
				<text class="appleLogin-text">通过Apple登录</text>
			</view>
			<!-- <view class="register-btn" @tap="register">
				<image class="register-icon" src="../../static/login/register-icon.png"></image>
				<view class="register-text">注册领新人礼</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	// import swiperItem from'../../components/login/swiper.vue'
	const Alibcsdk = uni.requireNativePlugin('UZK-Alibcsdk');
	export default {
		data() {
				return {
					  type:"",
						background: ['color1', 'color2', 'color3'],
						indicatorDots: true,
						autoplay: true,
						interval: 2000,
						duration: 500,
						bigTitle:"送鼓励金",
						smallTitle:"笔笔都送鼓励金",
						mainPic:'../../static/guide/Illustration.png',
						list:[
							{
								bigTitle:"购物先领券",
								smallTitle:"网购之前先搜券",
								mainPic:'../../static/login/quan.png',
							},
							{
								bigTitle:"送鼓励金",
								smallTitle:"笔笔都送鼓励金",
								mainPic:'../../static/login/coin.png',
							},
							{
								bigTitle:"分享赚钱",
								smallTitle:"帮人省 自己赚 赠人玫瑰 手有余香",
								mainPic:'../../static/login/share.png',
							},
							{
								bigTitle:"首单有礼",
								smallTitle:"完成首单即可提现",
								mainPic:'../../static/login/gift.png',
							},
						],
						system: '', // 系统版本
						platform: '',   // 平台
						isTrue:false
				}
		},
		components: {
			//swiperItem
		},
		onLoad(options) {
			//this.loadExecution();
			this.type = options.type;
			 // 先判断 系统版本
				uni.getSystemInfo({
						success: (res) => {
							if(res.system.indexOf('.') == -1){
								this.system = Number(res.system);
							}else{
								this.system = Number(res.system.split('.')[0]);
							}
								
								this.platform = res.platform;
								if(this.system >= 13 && this.platform == "ios"){
									this.isTrue = true
								}
							
						},fail: (err) => {
						},complete: () => {
						}
				})
		},
		methods: {
			loginApple(){
				//苹果登录
				 // 判断是 iOS13版本
				 var _this = this;
				uni.login({
						provider: 'apple',
						success: (loginRes) => {
							console.log(loginRes);
								uni.getUserInfo({
										provider: 'apple',
										success: (userInfoRes) => {
											const openId = userInfoRes.userInfo.openId;
											const data = {
															unionid:openId
														}
											
											uni.request({
												url:_this.$API.URL + _this.$API.userLogin,
												data:data,
												success(result){
													console.log(result)
													//登录成功之后获取用户信息，并缓存
													// _this.getUserInfo();
													
													uni.request({
														url:_this.$API.URL + _this.$API.userView,
														data:{
															apptoken:result.data.data.apptoken
														},
														success(res) {
																console.log(res)
																if(res.data.success){
																	uni.setStorage({
																		key:"UserInfo",
																		data:res.data.data
																	})
																}
														}
													})
													
													if(result.data.success){
														uni.setStorage({
															key:"apptoken",
															data:result.data.data.apptoken,
														})
														uni.setStorage({
															key:"isAutoTbao",
															data:result.data.data.relation_id
														})
														
														uni.setStorage({
															key:"userId",
															data:result.data.data.userId,
															success() {
																//uni.showToast("登录成功")
																uni.switchTab({
																	url:'../home/home',
																})
															}
														})
														console.log(result.data.data.apptoken);
														console.log(result.data.data.userId);
													}else{
														//uni.showToast("登录失败")
														_this.$CommonJS.showToast(result.data.msg)
													}
													
												}
											})
											
											
										}
								})
						},
						fail(res) {
							_this.$CommonJS.showToast(res)
						}
				})
				           
			},
			loginPhone(){
				uni.navigateTo({
					url:'./login-phone'
				})
			},
			register(){
				uni.navigateTo({
					url:'./register'
				})
			},
			closed(e){
				if(this.type == 1){
					uni.switchTab({
						url:'../home/home',
					})
				}else{
					uni.navigateBack({
						animationType:'slide-out-bottom',
						delta:1
					})
				}
				
			},
			// loadExecution: function(){
			// 	/**
			// 	 * 获取本地存储中launchFlag的值
			// 	 * 若存在，说明不是首次启动，直接进入首页；
			// 	 * 若不存在，说明是首次启动，进入引导页；
			// 	 */
			// 	const apptoken = uni.getStorageSync('apptoken');
			// 	console.log(apptoken);
			// 	if(apptoken){
			// 			uni.switchTab({
			// 			    url: '/pages/home/home'
			// 			});
			// 	}
			// },
			userLogin(){
				//微信登录
				var _this = this;
				uni.login({
				  provider: 'weixin',
				  success:  (loginRes) => {
				    console.log(loginRes);
						const unionid = loginRes.authResult.unionid;
				    // 获取用户信息
						//const _this = _this;
				    uni.getUserInfo({
				      provider: 'weixin',
				      success:  (infoRes) => {
								const data = {
												nickName:infoRes.userInfo.nickName,
												unionid:unionid,
												avatarUrl:infoRes.userInfo.avatarUrl
											}
											//_this.loginRequest();
											uni.request({
												url:_this.$API.URL + _this.$API.userLogin,
												data:data,
												success(result){
													console.log(result)
													//登录成功之后获取用户信息，并缓存
													// _this.getUserInfo();
													//_this.$emit('getMain')
													if(result.data.relation_id){
														uni.setStorage({
															key:"isAutoTbao",
															data:true
														})
													}else{
														uni.setStorage({
															key:"isAutoTbao",
															data:false
														})
													}
													
													if(result.data.success){
														const d = result.data.data;
														if(!d.phone){
															// uni.setStorage({
															// 	key:'testKey',
															// 	data:result.data.data.apptoken,
															// 	success() {
															// 		uni.navigateTo({
															// 			url:'./register-bindphone'
															// 		})
															// 	}
															// })
															uni.navigateTo({
																url:'./register-bindphone?apptoken='+d.apptoken+'&userId='+d.userId+'&isAutoTbao='+d.relation_id
															})
														}else{
															uni.request({
																url:_this.$API.URL + _this.$API.userView,
																data:{
																	apptoken:d.apptoken
																},
																success(res) {
																		console.log(res)
																		if(res.data.success){
																			uni.setStorage({
																				key:"UserInfo",
																				data:res.data.data
																			})
																		}
																}
															})
															
															uni.setStorage({
																key:"apptoken",
																data:d.apptoken,
															})
															
															// uni.setStorage({
															// 	key:"inviteId",
															// 	data:result.data.data.inviteId
															// })
															uni.setStorage({
																key:"userId",
																data:d.userId,
															})
															uni.setStorage({
																key:"isAutoTbao",
																data:d.relation_id,
																success() {
																	uni.switchTab({
																		url:'../home/home',
																	})
																}
															})
															
														}
														
														
													}else{
														//uni.showToast("登录失败")
														_this.$CommonJS.showToast(result.data.msg)
													}
													
												}
											})
									
				      }
				    });
				  },
					fail(res) {
						uni.showToast({
							title:"微信登录失败"
						})
					}
				});
			
			}
		},
		loginRequest(data){
			const _this = this;
			uni.request({
				url:_this.$API.URL + _this.$API.userLogin,
				data:data,
				success(result){
					console.log(result)
					//登录成功之后获取用户信息，并缓存
					_this.getUserInfo();
					if(result.data.success){
						uni.setStorage({
							key:"apptoken",
							data:result.data.data.apptoken,
						})
						uni.setStorage({
							key:"isAutoTbao",
							data:result.data.data.relation_id
						})
						uni.setStorage({
							key:"userId",
							data:result.data.data.userId,
							success() {
								//uni.showToast("登录成功")
								uni.switchTab({
									url:'../home/home',
								})
							}
						})
						console.log(result.data.data.apptoken);
						console.log(result.data.data.userId);
					}else{
						//uni.showToast("登录失败")
						_this.$CommonJS.showToast(result.data.msg)
					}
					
				}
			})
		},
		getUserInfo(){
			//获取用户信息
			var _this = this;
			uni.request({
				url:this.$API.URL + this.$API.userView,
				data:{
					apptoken:this.apptoken
				},
				success(res) {
						console.log(res)
						if(res.data.success){
							uni.setStorage({
								key:"UserInfo",
								data:res.data.data
							})
						}
				}
			})
		},
	}
</script>

<style>
	@import url("login.css");
</style>
